<?php
/* explain:		news system
 * Project:     
 * File:        news.php
 *
 * @ link 		http://www.../modules/
 * @ Email		ldmmyx@hotmail.com
 * @ copyright 	2005  Ling Deming
 * @ author 	ivan.ling  Ling Deming
 * @ version 	1.0
 *
 */
include_once("utils/CommonUtils.php");
include_once("constant/CommonClass.php");
include_once("constant/CommonDao.php");
include_once("dao/TagDao.php");

class TagImpl extends CommonUtils implements TagDao, CommonDao
{

	function __construct($DBTag)
	{
		parent::__construct();
		
		$this->DBTag = $DBTag;
	}

	/* get data via ID
	 *
	 * @param  int    id
	 * @return mixed  false:
	 */
	public function getById($id)
	{
		$sql  = 'SELECT id, name, status, r_popular, r_ContentCount ';
		$sql .= "FROM tags WHERE id = $id";
		$conn = null;
		
		try {
			$conn 	= $this->getConnection($this->DBTag);
			$rs 	= mysql_query($sql, $conn);
			if ($rs && $row = mysql_fetch_row($rs)) {
				$oTag = new TagDto();

				$oTag->id				= $row[0];
				$oTag->name				= $row[1];
				$oTag->status			= $row[2];
				$oTag->r_popular		= $row[3];
				$oTag->r_ContentCount	= $row[4];
			}
		} catch (Exception $e) {
			$this->messages = $e->getMessage();
		}
		
		$this->freeConnection($this->DBTag, $conn);
		return $oTag;
	}
	
	/* get data via ID
	 *
	 * @param  int    id
	 * @return mixed  false:
	 */
	public function getByName($name)
	{
		$sql  = 'SELECT id, name, status, r_popular, r_ContentCount ';
		$sql .= "FROM tags WHERE name = '" . addslashes($name) . "'";
		$conn = null;
		
		try {
			$conn 	= $this->getConnection($this->DBTag);
			$rs 	= mysql_query($sql, $conn);
			if ($rs && $row = mysql_fetch_row($rs)) {
				$oTag = new TagDto();

				$oTag->id				= $row[0];
				$oTag->name				= $row[1];
				$oTag->status			= $row[2];
				$oTag->r_popular		= $row[3];
				$oTag->r_ContentCount	= $row[4];
			}
		} catch (Exception $e) {
			$this->messages = $e->getMessage();
		}
		
		$this->freeConnection($this->DBTag, $conn);
		return $oTag;
	}

	public function addTags($tags)
	{
		$tags = trim($tags);
		if(empty($tags))
			return false;
			
		$arrTags = explode(',', str_replace('，', ',', $tags));
				
		$arrTagIds = array();
		foreach($arrTags as $tag)
		{
			$tag = trim($tag);
			
			$oTag = $this->getByName($tag);
			
			if(is_object($oTag)){
				$arrTagIds[] = $oTag->id;
			}else{
				$oTag = new TagDto();

				$oTag->name				= $tag;
				$oTag->status			= '1';
				$oTag->r_popular		= 0;
				$oTag->r_ContentCount	= 0;
				
				$id = $this->add($oTag);
				
				if($id > 0)
					$arrTagIds[] = $id;
			}
		}
		
		return $arrTagIds;
	}
	
	public function add($oTag)
	{		
		if($oTag instanceof TagDto){
			
			$sql 	 = "INSERT INTO tags SET name='";
			$sql	.= addslashes($oTag->name);
			
			$sql	.= "', status=";
			$sql	.= $oTag->status;
			
			$id = $this->insert($this->DBTag, $sql);
			
			$oTag->id = $id;
		}else{
			$id	= -1;
			
			$this->message = 'input object is not instance of TagDto.';
		}
		
		return $id;
	}
	
	public function edit($oTag)
	{		
		$id	= 0;
		
		if($oTag instanceof TagDto){
			
			$sql 	 = "UPDATE tags SET name='";
			$sql	.= addslashes($oTag->name);
			
			$sql	.= "', status=";
			$sql	.= $oTag->status;
			
			$sql	.= " WHERE id = ";
			$sql	.= $oTag->id;
			
			$this->update($this->DBTag, $sql);
		}else{
			$id	= -1;
			
			$this->message = 'input object is not instance of TagDto.';
		}
		
		return $id;
	}
	
	/* delete tag
	 *
	 * @param  int    	id
	 * @param  string  	status
	 * @param  return 	boolean
	 */
	public function delete($id)
	{
		$sql = "DELETE FROM tags WHERE id = $id";
		
		return $this->update($this->DBTag, $sql);
	}
	
	public function getTags($page = 0, $discount = 20)
	{
		$oTags = $this->getCustoms(" WHERE status = 1 AND r_contentCount > 0", " ORDER BY r_imageCount desc", $page, $discount);

		return $oTags;
	}
	
	public function getCategoryTags($categoryId, $page = 0, $discount = 20)
	{
		$where = " WHERE status = 1 AND r_imageCount > 0 AND id IN (SELECT tagId FROM category_tags WHERE categoryId = $categoryId)";
		$oTags = $this->getCustoms($where, " ORDER BY r_imageCount desc", $page, $discount);
		
		return $oTags;
	}
	
	/* list
	 *
	 * @ param  string 	class id
	 * @ param  string 	page
	 * @ param  string 	discount
	 * @ param  string 	url
	 * @ param  return 	array
	 */
	public function getCustoms($where, $order, $page = 0, $discount = 20)
	{
		$arrback 	= array();
		$conn 		= null;
		
		try{
			$conn 	= $this->getConnection($this->DBTag);
			
			$sql 	= "SELECT count(1) FROM tags $where";	
			$rs 	= mysql_query($sql, $conn) or die("<br><br>Invalid query: " . $sql . mysql_error($conn));

			list($total) = mysql_fetch_row($rs);
			
			$this->mutilPager($total, $page, $discount);
			
			if(!empty($total)){
				$sql  = 'SELECT id, name, status, r_popular, r_contentCount, r_imageCount';
				$sql .= " FROM tags  $where $order";
				$sql .= " LIMIT $this->firstcount, $discount";
				
				$rs  = mysql_query($sql, $conn);
				if($rs){
					while($row = mysql_fetch_array($rs)){
						$oTag = new TagDto();
		
						$oTag->id				= $row[0];
						$oTag->name				= $row[1];
						$oTag->status			= $row[2];
						$oTag->r_popular		= $row[3];
						$oTag->r_ContentCount	= $row[4];
						$oTag->r_ImageCount		= $row[5];
						
						$arrback[] = $oTag;
					}
				}else {
					$this->message = "select failed. sql : $sql";
					$this->message .= "<br>error: " . mysql_error($conn);
				}
			}
		}catch(Exception $e){
			$this->message = $e->getMessage();			
		}
		
		$this->freeConnection($this->DBTag, $conn);
		return $arrback;
	}
	

	public function getTagContentCount($DBTag)
	{
		$sql  = 'SELECT ct.tagId, COUNT(1) FROM content c, content_tags ct WHERE c.id = ct.contentId AND c.status = 1 GROUP BY ct.tagId';
			
		return $this->getHash($DBTag, $sql);
	}
	
	public function getTagImageCount($DBTag)
	{
		$sql  = 'SELECT it.tagId, COUNT(1) FROM images i, image_tags it WHERE i.id = it.imageId AND i.status = 1 GROUP BY it.tagId';
			
		return $this->getHash($DBTag, $sql);
	}
	
	public function getHasContentTags($DBTag)
	{
		$sql  = 'SELECT id, r_contentCount FROM tags WHERE status = 1 AND r_contentCount > 0';
			
		return $this->getHash($DBTag, $sql);
	}
	
	public function getHasImageTags($DBTag)
	{
		$sql  = 'SELECT id, r_imageCount FROM tags WHERE status = 1 AND r_imageCount > 0';
			
		return $this->getHash($DBTag, $sql);
	}
	
	public function getMessage() 
	{
		return $this->message;
	}

	
	
}
?>
